package com.kitty.game.skill.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.artifact.ArtifactService;
import com.kitty.game.config.Skill;
import com.kitty.game.fight.SkillConst;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Role;
import com.kitty.game.skill.SkillDataPool;
import com.kitty.game.skill.message.vo.SkillInfo;
import com.kitty.game.skill.message.vo.SkillResource;
import com.kitty.game.skill.model.RoleSkill;
import com.kitty.game.skill.model.SkillBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SkillService {
    Logger logger = LoggerFactory.getLogger(SkillService.class);
    /**
     * 读取配置技能表目标数量的技能id列表
     * 妖皇天怒、血炼地狱、血怒狂风、群蝎献祭
     */
    private List<Integer> targetNumSkillIds = Arrays.asList(701, 702, 703, 704);

    /**
     * 初始化技能数据
     *
     * @param list
     */
    public void initSkill(List<Skill> list) {
        Map<Integer, Skill> id2Skill = new HashMap<>(list.size());
        Map<String, Skill> name2Skill = new HashMap<>(list.size());
        for (Skill skill : list) {
            id2Skill.put(skill.getSkillId(), skill);
            name2Skill.put(skill.getSkillName(), skill);
        }
        SkillDataPool.id2Skill = id2Skill;
        SkillDataPool.name2Skill = name2Skill;
    }


    /**
     * 技能ID取技能公共信息
     */
    public Skill getSkillInfo(int skillId) {
        return SkillDataPool.id2Skill.get(skillId);

    }

    /**
     * 取个人技能信息
     *
     * @return
     */
    public RoleSkill getRoleSkill(Role role, int skillId) {
        return getRoleSkill(role, role.getPetBox().getFightPetId(), skillId);
    }

    /**
     * 取个人技能信息
     *
     * @return
     */
    public RoleSkill getRoleSkill(Role role, int fightPetId, int skillId) {
        RoleSkill skill = role.getSkillBox().querySkillBy(skillId);
        if (skill != null) {
            return skill;
        }
        ArtifactService artifactService = SpringUtils.getBean(ArtifactService.class);
        int artifactSkillId = artifactService.getSpecialSkillId(role);
        if (artifactSkillId == SkillConst.SKILL_QIN_MI) {
            Pet pet = role.getPetBox().getPetByPetId(fightPetId);
            if (pet != null) {
                return pet.getSkillBox().querySkillBy(skillId);
            }
        }
        return null;
    }


    /**
     * 取宠物技能信息
     *
     * @return
     */
    public RoleSkill getPetSkill(int skillId, Pet pet) {
        return pet.getSkillBox().querySkillBy(skillId);
    }

    /**
     * 学习技能
     */
    public void learn(Role role, RoleSkill roleSkill) {
        role.getSkillBox().learnSkill(roleSkill);
        role.save();
    }

    /**
     * 学习技能
     */
    public void petLearn(RoleSkill roleSkill, Role role, Pet pet) {
        pet.getSkillBox().learnSkill(roleSkill);
        role.save();
    }

    /**
     * 返回学习消耗潜能
     */
    public int getLearnPotNew(int currLevel, int learnLevel, int skillType, int skillRank) {
        int multi = learnLevel - currLevel;
        double per = 0.46, rankRate = 0.45;
        if (skillType == 147) {
            per = 0.38;
        } else if (skillType == 138) {
            per = 0.3;
        }
        if (skillRank == 2) {
            rankRate = 0.6;
        } else if (skillRank == 3) {
            rankRate = 0.75;
        } else if (skillRank == 4) {
            rankRate = 0.95;
        } else if (skillRank == 5) {
            rankRate = 1.1;
        }
        return (int) Math.max(Math.pow(currLevel, 3) * per * rankRate, 1) * multi;
    }

    /**返回被动技能消耗金钱*/
    public int getLearnCash(int currLevel, int learnLevel) {
        int cash = 0;
        int multi = learnLevel - currLevel;
        if (currLevel < 10) {
            if (currLevel == 0) {
                if (learnLevel > 1) {
                    cash = currLevel * 550000 + 515000 * learnLevel;
                } else {
                    cash = 200000;
                }
            } else {
                cash = currLevel * 550000 + 515000 * multi;
            }
        } else {
            cash = currLevel * 700000 + 515000 * multi;
        }
        return cash;
    }


    /**
     * 取技能消耗法力
     *
     * @return
     */
    public int getSkillMana(RoleSkill roleSkill) {
        int skillJieshu = roleSkill.getSkillJieshu();
        double skillLevel = roleSkill.getLevel();
        int skillType = roleSkill.getType();
        double mana = 0;
        if (roleSkill.getSkillId() == 501) {
            mana = 27.38;
            mana *= skillLevel;
        } else {
            if (skillType == 146) {//攻击技能
                if (skillJieshu == 1) {
                    mana = 10.1150;
                    mana *= skillLevel;
                } else if (skillJieshu == 2) {
                    mana = 16.14;
                    mana *= skillLevel;
                } else if (skillJieshu == 3) {
                    mana = 31.565;
                    mana *= skillLevel;
                } else if (skillJieshu == 4) {
                    mana = 39.4777;
                    mana *= skillLevel;
                } else if (skillJieshu == 5) {
                    mana = 51.325;
                    mana *= skillLevel;
                }
            } else if (skillType == 147) {//障碍技能
                if (skillJieshu == 1) {
                    mana = 14.2629;
                    mana *= skillLevel;
                } else if (skillJieshu == 2) {
                    mana = 19.9170;
                    mana *= skillLevel;
                } else if (skillJieshu == 3) {
                    mana = 30.7254;
                    mana *= skillLevel;
                } else if (skillJieshu == 4) {
                    mana = 38.7098;
                    mana *= skillLevel;
                } else if (skillJieshu == 5) {
                    mana = 60.3195;
                    mana *= skillLevel;
                }
            }
            if (skillType == 138) {//辅助技能
                if (skillJieshu == 1) {
                    mana = 12.0105;
                    mana *= skillLevel;
                } else if (skillJieshu == 2) {
                    mana = 20.1243;
                    mana *= skillLevel;
                } else if (skillJieshu == 3) {
                    mana = 27.7410;
                    mana *= skillLevel;
                } else if (skillJieshu == 4) {
                    mana = 27.3523;
                    mana *= skillLevel;
                } else if (skillJieshu == 5) {
                    mana = 49.0052;
                    mana *= skillLevel;
                }
            }
        }
        return new Double(mana).intValue();
    }

    /**
     * 取技能目标数
     *
     * @return
     */
    public int getSkillTargetNum(RoleSkill roleSkill) {
        Role role = SpringUtils.getRoleService().getOnlinePlayer(roleSkill.getRoleId());
        int zengyiLevel = 0;
        if (role != null) {
            zengyiLevel = role.getExtraSkillLevel();
        }
        Skill skill = getSkillInfo(roleSkill.getSkillId());
        int skillLevel = roleSkill.getLevel() + zengyiLevel;
        int targetNum = 0;
        if (roleSkill.getSkillId() == 501) {
            if (skillLevel >= 160) {
                targetNum = 4;
            } else if (skillLevel >= 120) {
                targetNum = 3;
            } else if (skillLevel >= 60) {
                targetNum = 2;
            } else {
                targetNum = 1;
            }
            if (role != null){
                ArrayList<Integer> arrayList = new ArrayList();
                for (Integer integer : role.getPetBox().getLipo()){
                    if (arrayList.contains(integer)){
                        continue;
                    }
                    arrayList.add(integer);
                }
                targetNum = targetNum + arrayList.size();
            }

            return targetNum;
        }
        if (roleSkill.getSkillJieshu() == 1) {
            targetNum = 1;
        } else if (roleSkill.getSkillJieshu() == 2) {
            if (roleSkill.getType() == 138) {
                if (skillLevel >= 40) {
                    targetNum = 2;
                } else {
                    targetNum = 1;
                }
            } else {
                if (skillLevel >= 30) {
                    targetNum = 2;
                } else {
                    targetNum = 1;
                }
            }

        } else if (roleSkill.getSkillJieshu() == 3) {
            if (skillLevel >= 160) {
                targetNum = 5;
            } else if (skillLevel >= 120) {
                targetNum = 4;
            } else if (skillLevel >= 60) {
                targetNum = 3;
            } else {
                targetNum = 2;
            }
        } else if (roleSkill.getSkillJieshu() == 4) {
            targetNum = 1;
        } else if (roleSkill.getSkillJieshu() == 5) {
            if (roleSkill.getType() == 138 ){
                if (skillLevel >= 180) {
                    targetNum = 7;
                } else if (skillLevel >= 160) {
                    targetNum = 6;
                } else if (skillLevel >= 120) {
                    targetNum = 5;
                } else if (skillLevel >= 80) {
                    targetNum = 4;
                } else {
                    targetNum = 3;
                }
            }else if (roleSkill.getType() == 147){
                if (skillLevel >= 180) {
                    targetNum = 7;
                    if (skill.getSkillJob() == 1){
                        targetNum = 5;
                    }
                } else if (skillLevel >= 160) {
                    targetNum = 6;
                    if (skill.getSkillJob() == 1){
                        targetNum = 5;
                    }
                } else if (skillLevel >= 120) {
                    targetNum = 5;
                } else if (skillLevel >= 80) {
                    targetNum = 4;
                } else {
                    targetNum = 3;
                }
            }else {
                if (skillLevel >= 160) {
                    targetNum = 5;
                } else if (skillLevel >= 120) {
                    targetNum = 4;
                } else if (skillLevel >= 80) {
                    targetNum = 3;
                } else {
                    targetNum = 2;
                }
                if (role != null){
                    ArrayList<Integer> arrayList = new ArrayList();
                    for (Integer integer : role.getPetBox().getFashu()){
                        if (arrayList.contains(integer)){
                            continue;
                        }
                        arrayList.add(integer);
                    }
                    targetNum = targetNum + arrayList.size();
                }
            }

//            if (roleSkill.getType() == 138 || (roleSkill.getType() == 147 && (skill.getSkillJob() == 3||skill.getSkillJob() == 4))) {
//                if (skillLevel >= 180) {
//                    targetNum = 7;
//                } else if (skillLevel >= 160) {
//                    targetNum = 6;
//                } else if (skillLevel >= 120) {
//                    targetNum = 5;
//                } else if (skillLevel >= 80) {
//                    targetNum = 4;
//                } else {
//                    targetNum = 3;
//                }
//            } else {
//                // 水和火七个 剩下都是五个
//                if (skillLevel >= 160) {
//                    targetNum = 5;
//                } else if (skillLevel >= 120) {
//                    targetNum = 4;
//                } else {
//                    targetNum = 2;
//                }
//            }
        }
        if(targetNum >= roleSkill.getTargetCount() ){

        }else{
            targetNum = roleSkill.getTargetCount();
        }
        if (targetNumSkillIds.contains(roleSkill.getSkillId())) {
            if (skill != null) {
                targetNum = skill.getMaxTarget();
            }
        }

        return targetNum == 0 ? 1 : targetNum;
    }

    /**
     * 取技能封包
     */
    public SkillInfo getSkillInfo(RoleSkill roleSkill, int extraLevel) {
        SkillService skillService = SpringUtils.getBean(SkillService.class);
        PetService petService = SpringUtils.getPetService();
        Skill skill = getSkillInfo(roleSkill.getSkillId());
        SkillInfo skillInfo = new SkillInfo();
        skillInfo.setResourceList(new ArrayList<>());
        skillInfo.setSkillId((short) roleSkill.getSkillId());
        skillInfo.setType((byte) skill.getSkillLife());
        if (skill.getSkillLife() == 1) {
            skillInfo.setLevel(roleSkill.getLevel());
            SkillResource resource = new SkillResource();
            resource.setResource("voucher_or_cash");//消耗类型 pot 潜能 cash 金钱
            resource.setUpgrade(skillService.getLearnCash(roleSkill.getLevel(), 1));
            skillInfo.getResourceList().add(resource);
        } else if (skill.getSkillType() == 146 || skill.getSkillType() == 147 || skill.getSkillType() == 138) {
            skillInfo.setLevel((short) (roleSkill.getLevel() + extraLevel));
            skillInfo.setZengyi((short) (extraLevel > 0 ? 1 : 0));
            SkillResource resource = new SkillResource();
            resource.setResource("pot");//消耗类型 pot 潜能 cash 金钱
            resource.setUpgrade(skillService.getLearnPotNew(roleSkill.getLevel(), roleSkill.getLevel() + 1, skill.getSkillType(), skill.getJeishu()));
            skillInfo.getResourceList().add(resource);
        } else if (skill.getSkillId() == 254 || skill.getSkillId() == 260 || skill.getSkillId() == 259
                || skill.getSkillId() == 255 || skill.getSkillId() == 258 || skill.getSkillId() == 261
                || skill.getSkillId() == 31 || skill.getSkillId() == 81 || skill.getSkillId() == 131
                || skill.getSkillId() == 181 || skill.getSkillId() == 231) {//天生技能
            skillInfo.setType((byte) 8);
            skillInfo.setLevel(roleSkill.getLevel());
            skillInfo.setSkill_nimbus(10000);
            SkillResource resource = new SkillResource();
            resource.setResource("party/contrib");//消耗类型 pot 潜能 cash 金钱
            resource.setUpgrade(petService.getCostContribPetUpgradeSkill(roleSkill.getLevel(), (short) 1));
            skillInfo.getResourceList().add(resource);

            resource = new SkillResource();
            resource.setResource("cash");//消耗类型 pot 潜能 cash 金钱
            resource.setUpgrade(petService.getCostCashPetUpgradeSkill(roleSkill.getLevel(), (short) 1));
            skillInfo.getResourceList().add(resource);
        } else if (skill.getSkillId() == 264 || skill.getSkillId() == 262 || skill.getSkillId() == 263 || skill.getSkillId() == 265) {
            /**研发技能*/
            skillInfo.setType((byte) 8);
            skillInfo.setLevel(roleSkill.getLevel());
            skillInfo.setSkill_nimbus(10000);

            SkillResource resource = new SkillResource();
            resource.setResource("party/contrib");//消耗类型 pot 潜能 cash 金钱
            resource.setUpgrade(petService.getCostContribPetUpgradeSkill(roleSkill.getLevel(), (short) 1));
            skillInfo.getResourceList().add(resource);

            resource = new SkillResource();
            resource.setResource("cash");//消耗类型 pot 潜能 cash 金钱
            resource.setUpgrade(petService.getCostCashPetUpgradeSkill(roleSkill.getLevel(), (short) 1));
            skillInfo.getResourceList().add(resource);
        }
        skillInfo.setSkillType((byte) roleSkill.getType());
        skillInfo.setMana((short) roleSkill.getMana());
        skillInfo.setTargetCount((short) skillService.getSkillTargetNum(roleSkill));
        skillInfo.setMaxTarget((short) skill.getMaxTarget());
        return skillInfo;
    }

    /**
     * 根据天书名称返回天书技能ID
     */
    public short getBookSkillId(String bookName) {
        switch (bookName) {
            case "怒击":
                return 1;
            case "烈炎":
                return 2;
            case "惊雷":
                return 3;
            case "青木":
                return 4;
            case "碎石":
                return 5;
            case "寒冰":
                return 6;
        }
        return 0;
    }

    /**
     * 获得技能ID列表
     */
    public List<Integer> getSkillIds(SkillBox skillBox) {
        List<RoleSkill> allSkills = skillBox.queryAllSkills();
        List<Integer> skillIds = new ArrayList<>(allSkills.size());
        for (RoleSkill roleSkill : allSkills) {
            skillIds.add(roleSkill.getSkillId());
        }
        return skillIds;
    }

}
